package cn.cupk.corporateshuttlemanagesystem.controller;

import cn.cupk.corporateshuttlemanagesystem.mapper.RouteInformationMapper;
import cn.cupk.corporateshuttlemanagesystem.mapper.LineInformationMapper;
import cn.cupk.corporateshuttlemanagesystem.pojo.Routes;
import cn.cupk.corporateshuttlemanagesystem.result.PageResult;
import cn.cupk.corporateshuttlemanagesystem.result.RequestParams;
import cn.cupk.corporateshuttlemanagesystem.result.Result;
import cn.cupk.corporateshuttlemanagesystem.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/lxxx")
@Slf4j
public class RouteInformationController {

    @Autowired
    private RouteInformationMapper routeInformationMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private LineInformationMapper lineInformationMapper;

    @PostMapping("/list")
    public Result<Object> page(@RequestBody RequestParams<Routes> params) {
        Page<Object> page = PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Routes> drivers = routeInformationMapper.page(params.getData());
        // 查询站点
        for (Routes route : drivers) {
            String stopsId = route.getStopsId();
            if (stopsId != null && stopsId.equals("")) {
                continue;
            }
            // 以逗号分隔
            String[] stationName = stopsId.split("，");
            List<Integer> stationId = new ArrayList<>();
            for (String s : stationName) {
                // 将 s 转换为 Integer 类型
                stationId.add(Integer.parseInt(s.trim()));
            }
            List<String> list = lineInformationMapper.stopList(stationId);
            route.setStationName(list);
        }
        return Result.ok(new PageResult<>(page.getTotal(), drivers));
    }

    @PostMapping("/add")
    public Result<Object> add(@RequestBody Routes routes) {
        // 查询线路是否存在
        Routes routes1 = routeInformationMapper.selectOne(routes.getName());
        routes.setStopsId("3，4");
        if (routes1 != null) {
            return Result.error("线路已存在");
        }
        // 查询公司是否存在
        if (userService.findCompanyById(routes.getCompanyId()) == null) {
            return Result.error("该公司未加入本系统");
        }
        Integer result = routeInformationMapper.add(routes);
        return Result.ok(result > 0 ? "添加成功" : "添加失败");
    }

    @PostMapping("/update")
    public Result<Object> update(@RequestBody Routes routes) {
        Routes routes1 = routeInformationMapper.selectOne(routes.getName());
        // 查询线路是否存在
        if (routes1 != null && !routes1.getRouteId().equals(routes.getRouteId())) {
            return Result.error("线路已存在");
        }
        if (userService.findCompanyById(routes.getCompanyId()) == null) {
            return Result.error("该公司未加入本系统");
        }
        Integer result = routeInformationMapper.update(routes);
        return Result.ok(result > 0 ? "修改成功" : "修改失败");
    }

}
